package com.novunda.platform.web;

import com.novunda.platform.common.exception.StatisticsException;
import com.novunda.platform.common.web.controller.BaseController;
import com.novunda.platform.entities.Audit;
import com.novunda.platform.services.AuditService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


/**
 * 审计Controller
 *
 * @author Lizy
 * @since 2016/9/8
 */
@Controller
@RequestMapping("/conf/audit")
public class AuditController extends BaseController {
    /**
     * 数据错误
     */
    public static final String ERROR_DATA = "数据格式错误!";
    /**
     * 保存成功
     */
    public static final String SAVE_SUCCEED = "保存成功!";
    @Autowired
    private AuditService auditService;

    @ModelAttribute("audit")
    @RequiresPermissions("sys:audit:view")
    public Audit get(@RequestParam(required = false) Long id) {
        if (id != null) {
            return auditService.getAudit();
        } else {
            return new Audit();
        }
    }

    @RequestMapping("")
    @RequiresPermissions("sys:audit:view")
    public String getAudit(Model model) {
        model.addAttribute("audit", auditService.getAudit());
        return "modules/conf/logsStatistics";
    }

    @RequestMapping("default")
    @RequiresPermissions("sys:audit:view")
    public String getDefaultAudit(Model model) {
        model.addAttribute("audit", auditService.getDefaultAudit());
        return "modules/conf/logsStatistics";
    }

    @RequestMapping(value = "save", method = RequestMethod.GET)
    @RequiresPermissions("sys:audit:edit")
    public String getSave(Model model) {
        return getAudit(model);
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @RequiresPermissions("sys:audit:edit")
    public String saveAudit(Audit audit, Model model, RedirectAttributes redirectAttributes) {
        // 检查数据格式是否正确
        if (!beanValidator(model, audit)) {
            addMessage(redirectAttributes, ERROR_DATA);
            model.addAttribute("message", ERROR_DATA);
            return "modules/conf/logsStatistics";
        }
        try {
            Audit audit1 = auditService.saveAudit(audit);
            addMessage(redirectAttributes, SAVE_SUCCEED);
            model.addAttribute("message", SAVE_SUCCEED);
            model.addAttribute("audit", audit1);
            return "modules/conf/logsStatistics";
        } catch (StatisticsException e) {
            LOG.error(e.getMessage());
            addMessage(redirectAttributes, e.getMessage());
            model.addAttribute("message", e.getMessage());
        }
        model.addAttribute("audit", audit);
        return "modules/conf/logsStatistics";
    }

}
